package com.qf.controller;

import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/coupon")
public class AdminCouponController {

    @Autowired
    DtsCouponService couponService;

    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon coupon){
        couponService.update(coupon);

        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon coupon){
        couponService.delete(coupon);

        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object create(@RequestBody Map<String,Object> map){
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        String tag = (String) map.get("tag");
        Integer total = Integer.valueOf(String.valueOf(map.get("total")));
        String discount = (String) map.get("discount");
        Integer min = Integer.valueOf(String.valueOf(map.get("min")));
        Integer limit = Integer.valueOf(String.valueOf(map.get("limit")));
        Integer type = Integer.valueOf(String.valueOf(map.get("type")));

        couponService.insert(name,desc,tag,total,discount,min,limit,type);

        return ResponseUtil.ok(map);
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       String name,String type,String status){
        Map<String,Object> data = couponService.findByPage(page,limit,sort,order,name,type,status);

        return ResponseUtil.ok(data);
    }
}
